﻿// -----------------------------------------------------------------------
// <copyright file="Enemy.cs" company="Diddly Squad">
// </copyright>
// -----------------------------------------------------------------------

using Microsoft.Xna.Framework;
using FarseerPhysics.Dynamics;

namespace DiddlySquat
{
	/// <summary>
	/// Base class for enemies
	/// </summary>
	public abstract class Enemy : Character
	{

		#region Enemy Properties

		public string EnemySprite { get; protected set; }
		public string DeathSprite { get; protected set; }
		
		public float EnemyRestitution { get; protected set; }
		public float EnemyFriction { get; protected set; }
		public double Damage { get; protected set; }
		public bool IsFlying { get; protected set; }
		public float Speed { get; protected set; }
		public float MaxSpeed { get; protected set; }
		public float Range { get; protected set; }
		public Enums.EnemyType EnemyType;
	   
		#endregion

		/// <summary>
		/// Updates the enemy and moves towards target.
		/// </summary>
		/// <param name="gameTime">The game time.</param>
		/// <param name="target">The target.</param>
		public virtual void Update(GameTime gameTime, Character target)
		{
			base.Update(gameTime);
			if (this.DistanceToTarget(target) < this.Range)
				this.Move(target);
		}

		/// <summary>
		/// Moves the specified target.
		/// </summary>
		/// <param name="target">The target.</param>
		public virtual void Move(Character target)
		{
			if ((this.Position.X > target.Position.X) && (this.Body.AngularVelocity > this.MaxSpeed * -(1)))
			{
				this.Body.ApplyTorque((this.Speed) * (-1));
			}

			if ((this.Position.X < target.Position.X) && (this.Body.AngularVelocity < this.MaxSpeed))
			{
				this.Body.ApplyTorque(this.Speed);
			}
		}

		/// <summary>
		/// Calculates distance to target.
		/// </summary>
		/// <param name="player">The player.</param>
		/// <returns>Calculated distance</returns>
		public float DistanceToTarget(Character player)
		{
			double dist = Utils.DistanceToTarget(this.Position, player.Position);
			return (float)dist;
		}

		/// <summary>
		/// Handles the the enemy and player collision.
		/// </summary>
		/// <param name="a">Initilizer</param>
		/// <param name="b">Target</param>
		/// <param name="c">Contact type</param>
		/// <returns></returns>
		protected override bool HandleCollision(Fixture a, Fixture b, FarseerPhysics.Dynamics.Contacts.Contact c)
		{
			if (b.UserData is Player)
			{
				var player = (Player)b.UserData;
				player.Hit(this.Damage);
			}
			return base.HandleCollision(a, b, c);
		}

		/// <summary>
		/// Initializes the max speed range.
		/// </summary>
		/// <param name="speed">The speed.</param>
		/// <param name="maxSpeed">The max speed.</param>
		/// <param name="range">The range.</param>
		public void InitializeSpeedMaxSpeedRange(float speed, float maxSpeed, float range)
		{
			this.Speed = speed;
			this.MaxSpeed = maxSpeed;
			this.Range = range;
		}
		
	}
}